package com.library.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.library.dao.BookDAO;
import com.library.entity.Book;

/**
 * 管理员图书管理Servlet
 */
//@webServlet("/admin/book")
public class AdminBookServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        // 设置请求和响应的字符编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        
        String action = request.getParameter("action");
        
        if (action == null) {
            // 默认显示所有图书
            showAllBooks(request, response);
        } else if (action.equals("add")) {
            // 显示添加图书表单
            request.getRequestDispatcher("/admin/add_book.jsp").forward(request, response);
        } else if (action.equals("edit")) {
            // 显示编辑图书表单
            editBook(request, response);
        } else if (action.equals("delete")) {
            // 删除图书
            deleteBook(request, response);
        } else if (action.equals("search")) {
            // 搜索图书
            searchBooks(request, response);
        } else {
            // 默认显示所有图书
            showAllBooks(request, response);
        }
    }
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        // 设置请求和响应的字符编码
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        
        String action = request.getParameter("action");
        
        if (action.equals("add")) {
            // 添加图书
            addBook(request, response);
        } else if (action.equals("edit")) {
            // 更新图书
            updateBook(request, response);
        } else {
            // 默认显示所有图书
            showAllBooks(request, response);
        }
    }
    
    // 显示所有图书
    private void showAllBooks(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        BookDAO bookDAO = new BookDAO();
        List<Book> books = bookDAO.getAllBooks();
        
        request.setAttribute("books", books);
        request.getRequestDispatcher("/admin/books.jsp").forward(request, response);
    }
    
    // 添加图书
    private void addBook(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        // 获取表单数据
        String isbn = request.getParameter("isbn");
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String publisher = request.getParameter("publisher");
        double price = Double.parseDouble(request.getParameter("price"));
        int stock = Integer.parseInt(request.getParameter("stock"));
        String category = request.getParameter("category");
        String coverImage = request.getParameter("coverImage");
        
        // 创建图书对象
        Book book = new Book(isbn, title, author, publisher, price, stock, category, coverImage);
        
        // 添加图书到数据库
        BookDAO bookDAO = new BookDAO();
        boolean success = bookDAO.addBook(book);
        
        if (success) {
            // 添加成功，重定向到图书列表
            response.sendRedirect("book");
        } else {
            // 添加失败，返回添加表单并显示错误信息
            request.setAttribute("error", "添加图书失败，请稍后再试");
            request.getRequestDispatcher("/admin/add_book.jsp").forward(request, response);
        }
    }
    
    // 显示编辑图书表单
    private void editBook(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        
        BookDAO bookDAO = new BookDAO();
        Book book = bookDAO.getBookById(id);
        
        request.setAttribute("book", book);
        request.getRequestDispatcher("/admin/edit_book.jsp").forward(request, response);
    }
    
    // 更新图书
    private void updateBook(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        // 获取表单数据
        int id = Integer.parseInt(request.getParameter("id"));
        String isbn = request.getParameter("isbn");
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String publisher = request.getParameter("publisher");
        double price = Double.parseDouble(request.getParameter("price"));
        int stock = Integer.parseInt(request.getParameter("stock"));
        String category = request.getParameter("category");
        String coverImage = request.getParameter("coverImage");
        
        // 创建图书对象
        Book book = new Book(isbn, title, author, publisher, price, stock, category, coverImage);
        book.setId(id);
        
        // 更新图书信息
        BookDAO bookDAO = new BookDAO();
        boolean success = bookDAO.updateBook(book);
        
        if (success) {
            // 更新成功，重定向到图书列表
            response.sendRedirect("book");
        } else {
            // 更新失败，返回编辑表单并显示错误信息
            request.setAttribute("error", "更新图书失败，请稍后再试");
            request.setAttribute("book", book);
            request.getRequestDispatcher("/admin/edit_book.jsp").forward(request, response);
        }
    }
    
    // 删除图书
    private void deleteBook(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        
        BookDAO bookDAO = new BookDAO();
        boolean success = bookDAO.deleteBook(id);
        
        if (success) {
            // 删除成功，重定向到图书列表
            response.sendRedirect("book");
        } else {
            // 删除失败，重定向到图书列表并显示错误信息
            request.setAttribute("error", "删除图书失败，请稍后再试");
            showAllBooks(request, response);
        }
    }
    
    // 搜索图书
    private void searchBooks(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String keyword = request.getParameter("keyword");
        
        BookDAO bookDAO = new BookDAO();
        List<Book> books = bookDAO.searchBooksByTitle(keyword);
        
        request.setAttribute("books", books);
        request.setAttribute("keyword", keyword);
        request.getRequestDispatcher("/admin/books.jsp").forward(request, response);
    }
}
